#include<iostream>
//#include <cctype>
#include <ctime>
#include<cstdio>
#include<cstring>
//#include"func.h"

#define MAXN 10000
#define MAXL 120000
int prime[MAXN];
int check[MAXL];



void Char_Grade(void){
    std::cout << "Current function: " << __func__ << std::endl;
    char a = 90;
    char b = 'Z';
    std::cout<< "char a = 90;"  << std::endl;
    std::cout<< "char b = 'Z';"  << std::endl;
    std::cout<< "char a = " << a << std::endl;
    std::cout<< "char b = " << b << std::endl;
    std::cout<< "(int)char a = " << (int)a << std::endl;
    std::cout<< "(int)char b = " << (int)b << std::endl;
    if(a == b){
        std::cout<< "a == b"  << std::endl;
    }
    else{
        std::cout<< "a != b"  << std::endl;
    }
}

void Case__Convertion(void){
    std::cout << "Current function: " << __func__ << std::endl;
    std::cout << "input a letter:" << std::endl;
    char a;
    std::cin>>a;
    std::cout << "original char =" << a << std::endl;
    if (a >= 'a' && a<= 'z'){
        if(a == 'z'){a = a - ('z' - 'A');}
        else{a = a - ('a' - 'B');}
    }
    else if(a >= 'A' && a<= 'Z'){
        if(a == 'Z'){a = a - ('Z' - 'a');}
        else{a = a - ('A' - 'b');}
    }
    else{
    std::cout << "输入不是英文字母。" << std::endl;
    return;
    }
    std::cout << "converted char =" << a << std::endl;
}

void Latitude_Convertion(void){
    std::cout << "Current function: " << __func__ << std::endl;
    float deg,min,sec;
    int err;
    while(1){
        err=0;
        std::cout << "input a latitude(Degrees, minutes, seconds):" << std::endl;
        std::cin>>deg >>min >>sec;
        if((deg>=0 && deg <=90)&&(min>=0 && min <60)&&(sec>=0 && sec <60)){
            deg=deg+(min+sec/60)/60;
        }
        else{
            err=-1;
        }
        if(deg>90){err=-1;}
        if(err == -1){ std::cout << "输入纬度不合法，请重新输入。" << std::endl; }
        else{break;}
    }
    std::cout << "converted latitude =" << deg << std::endl;
}
void func_4(void){
    std::cout << "见注释" << std::endl;
    /*
    int a = 2;
    int b = 5;
    char c = 80;
    float f = 3.2;
    short e;
    long l = 90;

    a/b=0,int除法向下取整
    b/a=2，同理
    输出 P 40 40 16,cout自动适应数据类型，除法返回类型为int
    a=f 结果为a=3，浮点数舍入
    f=f/b=0.64，浮点数不舍入
    f=(float)a/b=0.64,b自动转为float
    a=a/(int)f=0,f被转为int，执行int除法
    sizeof(a)=4;本机为4字节
    sizeof(long)=4;本机为4字节
    sizeof(float)=4;本机为4字节
    l=l/sizeof(long)=22,执行整数除法
    e=abcc=-1536,short型数据溢出
    l=abcc=64000，无溢出
    */
}
void func_5(void){
    std::cout << "见注释" << std::endl;
    /*
    int j = 5;
    while(++j <= 14)
    {
    std::cout << j++ << std::endl;
    }

    参考https://blog.csdn.net/qq_40024275/article/details/100524036
    循环每跑一次j+2;
    输出 5 7 9 11 13
    */
}

void Factorial(void){
    std::cout << "Current function: " << __func__ << std::endl;
    std::cout << "input a int num:" << std::endl;
    int a,i,out=1;
    std::cin>>a;
    std::cout << "original num =" << a << std::endl;
    for(i=1;i<=a;i++){
        out=out*i;
    }
    std::cout << "Factorial =" << out << std::endl;
}

void func_7(void){
    std::cout << "见注释" << std::endl;
    
    unsigned u1 = 25, u2 =38;
    std::cout << u2 - u1 << std::endl;
    std::cout << u1 - u2 << std::endl;
    int a1 = 25, a2 = 38;
    std::cout << a2 - a1 << std::endl;
    std::cout << a1 - a2 << std::endl;
    std::cout << a1 - u1 << std::endl;
    std::cout << u1 - a1 << std::endl;

    /*
    std::cout << u2 - u1 << std::endl;没问题
    std::cout << u1 - u2 << std::endl;无符号数减法溢出，cout按无符号输出
    std::cout << a2 - a1 << std::endl;没问题
    std::cout << a1 - a2 << std::endl;int型范围为-2147483468——2147483467，无溢出
    unsigned int 变量和int 变量在一起运算或比较时，会将int型转换为unsigned int型（算术运算的隐式转换）
    https://blog.csdn.net/RiceVan/article/details/108336848
    */
}

void Desks(void){
    std::cout << "Current function: " << __func__ << std::endl;
    std::cout << "input a int num:" << std::endl;
    int a,i,j;
    std::cin>>a;
    i = 0;
    j = 0;
    if(((a%10) == 7)||(((a/10)%10) == 7)||((a % 7) == 0)){
        i = 1;
    }
    if(((a%10) == 6)||(((a/10)%10) == 6)||((a % 6) == 0)){
        j = 1;
    }
    if(i == 1 && j == 1){
        std::cout << "没桌子" << std::endl;
    }
    else if (i == 1 && j == 0){
        std::cout << "敲桌子" << std::endl;
    }
    else if (i == 0 && j == 1){
        std::cout << "掀桌子" << std::endl;
    }
    else{
        std::cout << "无事发生" << std::endl;
    }
}

int getRand(int min, int max) {
    return ( rand() % (max - min + 1) ) + min ;
}

void Guess_Numbers(void){
    std::cout << "Current function: " << __func__ << std::endl;
    srand(time(0));
    std::cout << "input a int num:" << std::endl;
    int a,i,rand,n,min,max,maxguess,quit;
    char q;
    quit =0;
    maxguess =0;
    for(n=1;n>0;n++){
        min = 10*n*(-1);
        max = 10*n;
        rand = getRand(min,max);
        printf("第%d轮游戏,范围: %d - %d \n",n,min,max);
        for(i=1;i<=12;i++){
            std::cin>>a;
            while (std::cin.fail()) { //返回cin的状态
                std::cout << "输入非数字" << std::endl;
	            std::cin.clear();     //清除cin的错误状态
                std::cin>>q;
                if(q == 'q'){
                    quit = 1;
                    break;
                }
	            std::cin.ignore();    //忽略缓冲区的内容，直到EOF
	            std::cout << "输入错误，请重新输入" << std::endl;
	            std::cin >> a;      //重新输入
            }
            if(quit == 1){
                break;
            }

            if(a == rand){
                std::cout << "猜对了" << std::endl;
                break;
            }
            else if(a > rand){
                std::cout << "a > rand" << std::endl;
            }
            else{
                std::cout << "a < rand" << std::endl;
            }
        }
        maxguess = (i > maxguess)? i : maxguess;
        if((quit == 1)||(i == 12)){
            break;
        }
    }
    std::cout << "游戏结束" << std::endl;
    if(maxguess < 6){std::cout << "Chey-Yo!" << std::endl;}


}

void Narcissistic_numbers(void){
    std::cout << "Current function: " << __func__ << std::endl;
    int i;
    int a,b,c;
    for(i=100;i<=999;i++){
        a = i%10;
        b = (i/10)%10;
        c = (i/100)%10;
        if((a*a*a + b*b*b + c*c*c)==i){
            printf("%d是水仙花数\n",i);
        }
    }
}
void Goldbach_Conjecture(void){
    std::cout << "Current function: " << __func__ << std::endl;
    int i,p,q,end,a;
    int tot = 0;
    memset(check, 0, sizeof(check));

    for (i = 2; i < MAXL; ++i)
    {
        if(!check[i])prime[tot++] = i;
        for (int j = 0; j < tot; ++j)
        {
            if (i * prime[j] > MAXL) break;
            check[i*prime[j]] = 1;
            if (i % prime[j] == 0) break;
        }
    }
    //for(i=0;i<10000;i++){ printf("prime[%d]=%d\n",i,prime[i]);}
    for(i=0;i<=2000;i=i+2){
        end = 0;
        for(p=0;prime[p]<(i/2);p++){
            for(q=p;prime[q]<i;q++){
                if(prime[p]+prime[q] == i){
                    end = 1;
                    break;
                }
                if(end == 1){break;}
            }
            if(end == 1){break;}
        }
        printf("%d = %d + %d\n",i,prime[p],prime[q]);
    }
    std::cout << "input a int num:" << std::endl;
    std::cin>>a;
    std::cout << "original num =" << a << std::endl;
     end = 0;
    for(p=0;prime[p]<(a/2);p++){
        for(q=p;prime[q]<a;q++){
            if(prime[p]+prime[q] == a){
                end = 1;
                break;
            }
            if(end == 1){break;}
        }
        if(end == 1){break;}
    }
    printf("%d = %d + %d\n",a,prime[p],prime[q]);
    //https://blog.csdn.net/quchen528/article/details/93310565
    //https://blog.csdn.net/ITmincherry/article/details/105805135
}


void func_ctrl(void){
    std::cout << "Current function: " << __func__ << std::endl;
    int cmd;
    while(1){
        std::cin>>cmd;
        std::cout << "CMD =" << cmd << std::endl;
        switch(cmd){
            case 1:
                Char_Grade();
                break;
            case 2:
                Case__Convertion();
                break;
            case 3:
                Latitude_Convertion();
                break;
            case 4:
                func_4();
                break;
            case 5:
                func_5();
                break;  
            case 6:
                Factorial();
                break;  
            case 7:
                func_7();
                break;  
            case 8:
                Desks();
                break;  
            case 9:
                Guess_Numbers();
                break; 
            case 10:
                Narcissistic_numbers();
                break; 
            case 11:
                Goldbach_Conjecture();
                break;
            case 0:
                std::cout << "cmd =" <<cmd<<", return."<< std::endl;
                return;
        }
        std::cout << "Current function: " << __func__ << std::endl;
    }
}


int main() {
    system("chcp 65001");
    std::cout << "Hello World!" << std::endl;
    std::cout << "-------------------------------------------------------------------!" << std::endl;
    std::cout << "in week01 homework, choose the wanted function by typing in numbers:" << std::endl;

    func_ctrl();
    std::cout << "end test!" << std::endl;
    return 0;
}
